import Http from "../../../utils/http.js";
import Utils from '../../../utils/util.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      dicts:"",
      isRefresh:false,
      seachKeyWord:"",
      info: null,
      page: 1,
      loading: false,
      pageSize:10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取字段
    Http.request({
      url:"/dict/findDict",
      method:"GET",
      data:{
        category:'MALL_VEHICLE_STATUS'
      },
      success:(res)=>{
        this.setData({
          dicts:res
        })
      }
    });
  },
  onShow:function(){
    this.setData({
      page:1
    });
    this._queryCarData();
  },
  bindCarToTop:function(e){
    var dataSet = e.currentTarget.dataset;
    console.log('车辆所属地，携带值为', dataSet);
    
    Http.request({
      url:"/vehicleInfo/setTop",
      method:"POST",
      data:{
        id:dataSet.id
      },
      success:(res)=>{
        wx.showToast({
          title: '置顶车辆成功',
        })
        this.setData({
          page:1,
          info:""
        })
        this.onShow();
      }
    });
  },

  pageCaResultChangeRefresh:function(isRefresh){
      if(isRefresh){
        this._queryCarData();
        this.setData({
          isRefresh:false
        }) 
      }
  },

  bindCarPutOff:function(e){
    var dataSet = e.currentTarget.dataset;
    Http.request({
      url:"/vehicleInfo/putOff",
      method:"POST",
      data:{
        id:dataSet.id
      },
      success:(res)=>{
        wx.showToast({
          title: '车辆下架成功',
        })
        this.setData({
          page:1,
          info:""
        })
        this.onShow();
      }
    });
  },

  bindCarCancelTop:function(e){
    var dataSet = e.currentTarget.dataset;
    console.log(dataSet);
    Http.request({
      url:"/vehicleInfo/setUntop",
      method:"POST",
      data:{
        id:dataSet.id
      },
      success:(res)=>{
        wx.showToast({
          title: '取消置顶成功',
        })
        this.setData({
          page:1,
          info:""
        })
        this.onShow();
      }
    });
  },
  /**
   * 删除搜索文字按钮
   */
  bindClearSearchKeyWord:function(){
    this.setData({
      seachKeyWord:"",
      info: null,
      page: 1,
    });
    this._queryCarData();
  },
  /**
   * 搜索input输入事件
   */
  bindSeachInput:function(e){
    this.setData({
      seachKeyWord:e.detail.value
    });

    
  },
  bindSearchSubmit:function(){
    this.setData({
      page:1
    });
    this._queryCarData();
  },

  

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loading || (this.data.info.content.length == this.data.info.totalElements)) return;
    this._queryCarData("pullup");
  },
  
  _queryCarData(type){
    this.setData({
      loading: true
    })
    wx.showLoading({
      title: '',
    })
    Http.request({
      url: '/vehicleInfo/query/page',
      method:'GET',
      data:{
        pageSize:this.data.pageSize,
        pageNumber:this.data.page,
        keywords:this.data.seachKeyWord
      },
      success: (res) => {
        console.log(res);
        wx.hideLoading()
        var sessionId=wx.getStorageSync("sessionid");
        res.content=Utils.transformDict(res.content,this.data.dicts,"status");
        res.content=Utils.transformImageSrcBase64(res.content,"image");
        console.log(res);
        if (this.data.page == 1) {
          if (type == "pullup") {
            this.setData({
              ["info.content"]: res.content,
              ["info.totalPages"]: res.totalPages
            })
          } else {
            this.setData({
              info: res
            })
          }
        } else {
          this.setData({
            ["info.content"]: this.data.info.content.concat(res.content)
          })
        }
        this.setData({
          loading: false,
          page: this.data.page + 1,
        })
        wx.stopPullDownRefresh() //停止下拉刷新
      },
      error:function(){
        wx.hideLoading()
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },
   /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
    this.setData({
      page:1
    })
    this._queryCarData();
  }

})